# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, İlker Kesen <ilker.kde@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.

import variables as scroppy
import piksemel
import urllib2

class Album:
    def __init__(self, artist, album):
        (artist, album) = (artist.replace(" ", "%20"), album.replace(" ", "%20"))
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/album/%s/%s/info.xml" % (scroppy.url, scroppy.scrobblerVersion, artist, album)).read())

    def artist(self):
        return self.data.getAttribute("artist")

    def album(self):
        return self.data.getAttribute("title")

    def URL(self):
        return self.data.getTagData("url")

    def reach(self):
        return self.data.getTagData("reach")

    def releaseDate(self):
        return self.data.getTagData("releasedate")

    def coverart(self):
        small  = self.data.getTag("coverart").getTagData("small")
        medium = self.data.getTag("coverart").getTagData("medium")
        large  = self.data.getTag("coverart").getTagData("large")
        return {"large" : large, "medium" : medium, "small": small }

    def tracks(self):
        (tracks, tags, variable) = ([], self.data.getTag("tracks").tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                track = variable.getAttribute("title")
                reach = variable.getTagData("reach")
                url   = variable.getTagData("url")
                tracks.append((track, reach, url))
        return tracks
